using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using newsApi.Helpers;
using newsApi.Models;
using newsApi.Services;
using webAnewsApi.Filters;
using static newsApi.Services.UsersService;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.Configure<NewsLibDatabaseSettings>(builder.Configuration.GetSection("NewsLibDatabase"));
builder.Services.Configure<JWTKeySettings>(builder.Configuration.GetSection("JWTKey"));
builder.Services.AddSingleton<ChanelsService>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddCors(options =>
{
    options.AddDefaultPolicy(
        policy =>
        {
            policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
        });
});
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddAuthentication(auth =>{
    auth.DefaultAuthenticateScheme=JwtBearerDefaults.AuthenticationScheme;
    auth.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    }
).AddJwtBearer(options =>{
    options.SaveToken=true;
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuer = true,
        // ValidIssuer = config.JwtToken.Issuer,
        // ValidateAudience = true,
        // ValidAudience = config.JwtToken.Audience,
        // ValidateIssuerSigningKey = true,
        // IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config.JwtToken.SigningKey))
    };
});
builder.Services.AddSwaggerGen(
//     c=>{
//     c.OperationFilter<AddResponseHeadersFilter>();
//     c.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
// }
);
builder.Services.AddMvc(options =>
    {
        options.Filters.Add<ApiResultFilterAttribute>();
    });
 
builder.WebHost.ConfigureKestrel(options=>
{
    // options.ListenAnyIP(7161,config =>
    // {config.UseHttps(@"C:\Program Files\OpenSSL-Win64\bin\10.83.232.5.pfx","123456");
    // });
}
);
var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseMiddleware<JwtMiddleware>();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();



app.MapControllers();

app.Run();
